<?php
/**
 * Created by PhpStorm.
 * User: huangpeng3
 * Date: 2018/1/25
 * Time: 下午3:53
 * 策略模型
 */

namespace Home\Model;

use Think\Model;

class StrategyModel extends Model
{
    protected $tableName = 'strategies';

    public function getAll($uid)
    {
        $rst = $this->where('uid=' . $uid)->order('ctime desc')->getField('id, uid, name, private, sell,buy, timeout, mtime, ctime');
        if (isset($rst)) {
            $rtn = array();
            foreach ($rst as $k => $v) {
                array_push($rtn, $v);
            }
            return $rtn;
        } else {
            return -1;
        }
    }

    public function addStrategy($uid, $name, $code, $ctime)
    {
        $data = array(
            'uid' => $uid,
            'name' => $name,
            'code' => $code,
            'ctime' => $ctime
        );
        if ($this->add($data)) {
            return 200;
        } else {
            return -1;
        }
    }

    public function delStrategy($uid, $pid)
    {
        $data = array(
            'uid' => $uid,
            'id' => $pid
        );
        if ($this->where($data)->select()) {
            if ($this->where($data)->delete()) {
                return 200;
            }
        }
        return -1;
    }

    public function updateStrategy($pid, $uid, $name, $code, $mtime)
    {
        $where = array(
            'id' => $pid,
            'uid' => $uid,

        );
        $data = array(
            'name' => $name,
            'code' => $code,
            'mtime' => $mtime
        );
        if ($this->where($where)->select()) {
            if ($this->where($where)->save($data)) {
                return 200;
            }
        }
        return -1;
    }


}